package org.dormi.learn.product.domain.product.entity;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.dormi.learn.pojo.BaseEO;
import org.dormi.learn.primitive.Money;
import org.dormi.learn.utils.Assert;
import org.dormi.learn.utils.TrackingUtils;

import java.util.List;

/**
 * add description here
 *
 * @author : dormi330
 * @version : 1.0
 */
@Getter
@Setter
public class Product extends BaseEO {

    private Long id;
    private ProductCategory category;

    private Boolean canBeSold = false;
    private Boolean canBePurchased = false;
    private String name;
    private String materialCode;
    private String barcode;
    private Money salesPrice;
    private List<ProductVariant> variants;

    /* === internal biz code === */
    /* === internal biz code === */
    /* === internal biz code === */

    /** 完整性校验 */
    public void checkIntegrity() {
        TrackingUtils.debugTrace();
        // not null check
        Assert.isNotNull(category, "不能为null: category");
        Assert.isNotNull(name, "不能为null: name");

        if (!hasVariant()) {
            // 如果仅有1个规格, 则 materialCode 不能为空
            Assert.isNotNull(materialCode, "不能为null: materialCode");
            Assert.isNotNull(salesPrice, "不能为null: salesPrice");
        } else {
            // 多规格
            for (ProductVariant variant : variants) {
                variant.checkIntegrity();
            }
        }
    }


    /** 是否单规格 */
    private boolean hasVariant() {
        return !CollectionUtils.isEmpty(variants);
    }

}
